/*
 * Adara WebCam TimeLapse capture software.
 * Copyright (C) 2012 AdaraProjectTeam
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package adara.cli;

import java.io.File;
import java.util.List;

import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.log4j.Logger;

import adara.core.AdaraCore;
import adara.core.CoreException;
import adara.core.Device;
import adara.core.Process;

/**
 * The launcher of the application. Contains main method.
 * @author Andrei Ermicioi (aka erani) <andrei.ermicioi@gmail.com>
 *
 */
public class CLILauncher {
	
	private static final Logger logger = Logger.getLogger(CLILauncher.class);
	
	private static void printHelp(Options options) {
		StringBuilder sb = new StringBuilder("adara ");
		sb.append("[-h] [-l] [-i -d <device>] ")
			.append("[-c -d <device> -g <seconds> -s <storage> -t <seconds>]");
		
		HelpFormatter hformater = new HelpFormatter();
		hformater.printHelp(sb.toString(), options);
	}
	
	private static void printDeviceList() {
		List<Device> devices = null;
		
		try {
			devices = AdaraCore.getDeviceList();
		} catch (CoreException e) {
			logger.fatal("An exception occure on getting the device list", e);
			return;
		}
		
		if (devices.size() <= 0) {
			System.out.println("No devices found");
			return;
		}
		
		System.out.println("Device list");
		
		for (Device dev : devices) {
			printDeviceInfo(dev);
		}
	}
	
	private static void printDeviceInfo(int seq) {
		Device dev = null;
		
		try {
			dev = AdaraCore.getDeviceBySequence(seq);
		} catch (CoreException e) {
			logger.fatal("Could not obtain information on device", e);
			return;
		}
		
		System.out.println("Device info");
		printDeviceInfo(dev);
	}
	
	private static void printDeviceInfo(Device dev) {
		String lineSeparator = System.getProperty("line.separator");
		StringBuilder sbuilder = new StringBuilder();
		sbuilder.append("------")
			.append(lineSeparator)
			.append("Seqence: ")
			.append(dev.getSequence())
			.append(lineSeparator)
			.append("Device ID: ")
			.append(dev.getDeviceId())
			.append(lineSeparator)
			.append("Device description: ")
			.append(dev.getDeviceDescription());
		
		System.out.println(sbuilder.toString());
	}
	
	private static void runCapture(int seq, int gap, File storage, int duration) {
		Device device = null;
		
		try {
			device = AdaraCore.getDeviceBySequence(seq);
		} catch (CoreException e) {
			logger.fatal("Could not obtain device by sequence number");
			return;
		}
		
		Process process = null;
		
		try {
			process = AdaraCore.factoryProcess(device, gap, storage);
		} catch (CoreException e) {
			logger.fatal("Could not factory process by sequence", e);
		}
		
		process.start();
		
		try {
			Thread.sleep(duration * 1000);
		} catch (InterruptedException e) {
			logger.error("Waiting process has be interupted", e);
		}
		
		process.stop();
	}
	
	private static void printLegalNotice() {
		String separator = System.getProperty("line.separator");
		StringBuilder sb = new StringBuilder(
				"Adara WebCam TimeLapse  Copyright (C) 2012 AdaraProjectTeam");
		sb.append(separator)
			.append("This program comes with ABSOLUTELY NO WARRANTY.")
			.append(separator)
			.append("This is free software, and you are welcome to redistribute it.")
			.append(separator);
		System.out.println(sb.toString());
	}

	public static void main(String argc[]) {
		printLegalNotice();
		
		CLICommand command = null;
		
		try {
			command = CLICommand.factory(argc);
		} catch (CLIException e) {
			logger.fatal("An exception occur on getting the execution command", e);
			return;
		}
		
		switch (command.getCommand()) {
			case HELP:
				printHelp(command.getOptions());
				break;
				
			case LIST:
				printDeviceList();
				break;
				
			case INFO:
				printDeviceInfo(command.getDevice());
				break;
				
			case CAPTURE:
				runCapture(command.getDevice(), command.getGap(),
						command.getStorage(), command.getTime());
				break;
				
			default:
				logger.error("Undefined message in command line");
		}
	}
}
